from stark.service.v1 import StarkHandler, get_date_format_text, get_m2m_text, StarkModelForm
from web import models
from django import forms
from django.utils.safestring import mark_safe
from django.urls import reverse
from .base import PermissionHandler

# 重写类的 方法，指定字段样式的显示
class DatetimePickerInput(forms.TextInput):
    template_name = 'stark/forms/widgets/datetime_picker.html'


class ClassListModelForm(StarkModelForm):
    class Meta:
        model = models.ClassList
        fields = '__all__'
        widgets = {
            "start_date": DatetimePickerInput,  # 默认字段格式是 textinput
            "graduate_date": DatetimePickerInput,
        }


class ClassListHandler(PermissionHandler,StarkHandler):

    def display_class(self, obj=None, is_header=None, *args, **kwargs):
        if is_header:
            return '班级'
        return '%s %s期' % (obj.course.title, obj.semester)

    def display_class_record(self, obj=None, is_header=None, *args, **kwargs):
        if is_header:
            return '上课记录'
        url = reverse('stark:web_courserecord_list', kwargs={'class_id': obj.pk})
        return mark_safe('<a target="_blank" href="%s">上课记录</a>' % url)

    field_name_list = ['school',
                       display_class,
                       'price',
                       get_date_format_text('开班日期', 'start_date'),
                       get_m2m_text('授课老师', 'tech_teacher'),
                       display_class_record,
                       ]
    model_form_class = ClassListModelForm
